package com.example.yuejian.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.example.yuejian.R;
import com.example.yuejian.bean.IsCollection;
import com.example.yuejian.bean.TrainDetailEntity;
import com.example.yuejian.common.Url;
import com.example.yuejian.view.TitleView;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;

import butterknife.BindView;
import butterknife.OnClick;

public class TrainDetailActivity extends BaseActivity {


    private Intent intent;
    private String tid, uid;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_train_detail);
        super.onCreate(savedInstanceState);

        initView();
        initData();
    }

    @OnClick(R.id.bm)
    void bm(){
        Intent intent = new Intent(getBaseContext(),TrainDetailActivityGM.class);
        intent.putExtra("tid",tid);
        ActivityUtils.startActivity(intent);
    }

    Handler handler_activity = new mHandler();

    private void initData() {

        EasyHttp.get("yuejian6/public/index.php/json/train/isCollection")
                .params("train_id", tid).params("user_id", uid)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onSuccess(String data) {

                        Message msg = Message.obtain(); // 实例化消息对象
                        msg.what = 1; // 消息标识
                        msg.obj = data; // 消息内容存放

                        handler_activity.sendMessage(msg);
                    }

                    @Override
                    public void onError(ApiException error) {

                    }
                });


        EasyHttp.get("yuejian6/public/index.php/json/train/details")
                .params("train_id", tid)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onSuccess(String data) {

                        Message msg = Message.obtain(); // 实例化消息对象
                        msg.what = 0; // 消息标识
                        msg.obj = data; // 消息内容存放

                        handler_activity.sendMessage(msg);
                    }

                    @Override
                    public void onError(ApiException error) {

                    }
                });


    }

    class mHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    TrainDetailEntity trainDetailEntity = null;
                    try {
                        trainDetailEntity=JSON.parseObject(msg.obj.toString(), TrainDetailEntity.class);
                    }catch (Exception e){
                        ToastUtils.showShort("获取数据失败");
                        return;
                    }
                    tel.setText("电话： " + trainDetailEntity.getTel());
                    name.setText("名称： " + trainDetailEntity.getTrain_name());
                    address.setText("地址： " + trainDetailEntity.getAddress());
                    price.setText("价格： " + trainDetailEntity.getPrice() + "元");
                    Glide.with(context).load(Url.imgUrl + trainDetailEntity.getPic()).into(imageView);
                    time.setText("时间： " + trainDetailEntity.getTime());
                    intro.setText("介绍:\n" + trainDetailEntity.getIntro());
                    break;
                case 1:

                    IsCollection isCollection = null;
                    try {
                        isCollection=JSON.parseObject(msg.obj.toString(), IsCollection.class);
                    }catch (Exception e){
                        ToastUtils.showShort("获取数据失败");
                        return;
                    }
                    if ("0".equals(isCollection.getError())) {
                        rightTextTv.setText("已收藏");
                    } else {
                        rightTextTv.setText("收藏");
                    }

                    break;

                case 2:
                    IsCollection collection = null;
                    try {
                        collection=JSON.parseObject(msg.obj.toString(), IsCollection.class);
                    }catch (Exception e){
                        ToastUtils.showShort("获取数据失败");
                        return;
                    }
                    if ("0".equals(collection.getError())) {
                        ToastUtils.showShort("收藏成功");
                        rightTextTv.setText("已收藏");
                    } else {
                        rightTextTv.setText("收藏");
                    }
                    break;

                case 3:
                    IsCollection uncollection = null;
                    try {
                        uncollection=JSON.parseObject(msg.obj.toString(), IsCollection.class);
                    }catch (Exception e){
                        ToastUtils.showShort("获取数据失败");
                        return;
                    }
                    if ("0".equals(uncollection.getError())) {
                        ToastUtils.showShort("取消收藏成功");
                        rightTextTv.setText("收藏");
                    } else {
                        rightTextTv.setText("已收藏");
                    }
                    break;
            }


        }
    }

    @BindView(R.id.title_activity)
    TitleView title_activity;
    TextView rightTextTv;

    private static final String TAG = "TrainDetailActivity";
    private void initView() {

        context = getBaseContext();
        intent = getIntent();
        title_activity.getLeftBackTextTv().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityUtils.finishActivity(TrainDetailActivity.class);
            }
        });
        tid = intent.getIntExtra("tid", 0) + "";
        uid = SPUtils.getInstance().getInt("uid", 0) + "";

        Log.d(TAG, "initView: uid:"+uid+" ,tid: "+tid);

        rightTextTv = title_activity.getRightTextTv();

        rightTextTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("收藏".equals(rightTextTv.getText().toString())){

                    EasyHttp.get("yuejian6/public/index.php/json/train/collection")
                            .params("train_id", tid).params("user_id", uid)
                            .execute(new SimpleCallBack<String>() {
                                @Override
                                public void onSuccess(String data) {

                                    Message msg = Message.obtain(); // 实例化消息对象
                                    msg.what = 2; // 消息标识
                                    msg.obj = data; // 消息内容存放

                                    handler_activity.sendMessage(msg);
                                }

                                @Override
                                public void onError(ApiException error) {

                                }
                            });

                }else{
                    EasyHttp.get("yuejian6/public/index.php/json/train/unCollection")
                            .params("train_id", tid).params("user_id", uid)
                            .execute(new SimpleCallBack<String>() {
                                @Override
                                public void onSuccess(String data) {

                                    Message msg = Message.obtain(); // 实例化消息对象
                                    msg.what = 3; // 消息标识
                                    msg.obj = data; // 消息内容存放

                                    handler_activity.sendMessage(msg);
                                }

                                @Override
                                public void onError(ApiException error) {

                                }
                            });
                }
            }
        });

    }

    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.address)
    TextView address;
    @BindView(R.id.tel)
    TextView tel;
    @BindView(R.id.price)
    TextView price;
    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.imageView)
    ImageView imageView;
    @BindView(R.id.intro)
    TextView intro;

}
